export function getStyle(el: HTMLElement, attr: string, isNumber = false) {
  const style = getComputedStyle(el);
  let value: string | number = style.getPropertyValue(attr);
  if (isNumber) {
    value = value.replace("px", "");
    value = Number(value);
  }
  return value;
}

export function on(
  el: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject,
  capture = false
) {
  if (el && event && handler) {
    el.addEventListener(event, handler, capture);
  }
}
export function off(
  el: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject,
  capture = false
) {
  if (el && event && handler) {
    el.removeEventListener(event, handler, capture);
  }
}

/**
 * 截取html字符串中的前段的一部分
 * @param html 要截取的html字符串
 * @param num 截取的字符串数量
 */
export function getFrontDomText(html: string, num = 20) {
  let div: HTMLDivElement | null = document.createElement("div");
  div.innerHTML = html;
  const text = div.innerText.replace(/\s/g, " ");
  div = null;
  let suffix = "";
  if (text.length > num) {
    suffix = "......";
  }
  return text.substring(0, num - 1) + suffix;
}

/**
 * 寻找目标元素和目标元素的所有祖先元素中符合条件的元素（忽略body元素）
 * @param el 目标元素
 * @param handler 条件判断函数，返回true则说明符合
 * @returns 找到的元素，找不到返回null
 */
export function findParent(el: HTMLElement, handler: (target: HTMLElement) => boolean) {
  if (handler(el)) return el;
  let parent = (el.parentNode || el.parentElement) as HTMLElement;
  while (parent && parent.tagName.toLowerCase() !== "body") {
    if (handler(parent)) return parent;
    parent = parent.parentNode as HTMLElement;
  }
  return null;
}

//获取元素的纵坐标 相对于pNode 不传则是相对于body
function getTop(el: HTMLElement, pNode?: HTMLElement) {
  let offset = el.offsetTop;
  const offsetParent = el.offsetParent as HTMLElement;
  if (pNode && offsetParent === pNode) return offset;
  if (offsetParent !== null) {
    offset += getTop(offsetParent, pNode);
  }
  return offset;
}
//获取元素的横坐标 相对于pNode 不传则是相对于body
function getLeft(el: HTMLElement, pNode?: HTMLElement) {
  let offset = el.offsetLeft;
  const offsetParent = el.offsetParent as HTMLElement;
  if (pNode && offsetParent === pNode) return offset;
  if (offsetParent != null) offset += getLeft(offsetParent, pNode);
  return offset;
}

/**
 * 获取元素相对于某个祖先元素的坐标，若不指定祖先元素则是相对body节点
 * @param el 目标元素
 * @param pNode 祖先元素
 */
export function getElRelativeXY(el: HTMLElement, pNode?: HTMLElement) {
  return {
    x: getLeft(el, pNode),
    y: getTop(el, pNode),
  };
}

export function handleEvent(e: MouseEvent | TouchEvent) {
  let result: {
    x: number;
    y: number;
    x1: number;
    y1: number;
    isDoubleFinger: boolean;
  };
  if (e instanceof MouseEvent) {
    result = {
      x: e.pageX,
      y: e.pageY,
      x1: 0,
      y1: 0,
      isDoubleFinger: false,
    };
  } else {
    const changedTouches = e.changedTouches;
    result = {
      x: changedTouches[0].pageX,
      y: changedTouches[0].pageY,
      x1: changedTouches[1] ? changedTouches[1].pageX : 0,
      y1: changedTouches[1] ? changedTouches[1].pageY : 0,
      isDoubleFinger: changedTouches.length > 1,
    };
  }
  return result;
}
